package com.wuhanins.insmytestdemo.extend.webApiProxy.insSysProxy.getCircleRangeProxy;

import com.wuhanins.core.promise.Promiser;
import com.wuhanins.core.proxy.ProviderContext;
import com.wuhanins.core.proxy.base.BaseProxy;
import com.wuhanins.core.proxy.event.IOnProxyDoneListener;
import com.wuhanins.core.proxy.provider.IProxyDataProvider;
import com.wuhanins.http.volley.v4.IHttpHelperListener_v4;
import com.wuhanins.http.volley.v3.IOnProxyDoneForStringListener_v3;
import com.wuhanins.http.volley.v3.IOnProxyDoneListener_v3;

public class GetCircleRangeProxyApi extends BaseProxy {

	private String apiUrl = "api/v1/hr/location/CircleRange";
	private String method = "POST";

	public GetCircleRangeProxyApi(){
	    initProxy();
	}
	
	public GetCircleRangeProxyApi(ProviderContext pContext){
	    initProxy(pContext);
	}

	public void doRequest(
	    IProxyDataProvider provider,
	    GetCircleRangePInput pInput,
	    final IOnProxyDoneListener_v3<GetCircleRangeOutput> onListener
	){

	    Promiser $q = $doRequest(provider,
	    pInput,
	    onListener);
	    if($q != null)
	        $q.run();
	}

	public Promiser $doRequestForString(
	    IProxyDataProvider provider,
	    GetCircleRangePInput pInput,
	    final IOnProxyDoneForStringListener_v3 onListener
	){

	    String url = getApiURL();
	    ProviderContext pContext = getPContext();
	    pContext.setHost(url);
	    
	    IHttpHelperListener_v4 l = new IHttpHelperListener_v4() {
	            @Override
	            public void onSuccess(String result) {

	                onReSuccess(result);

	            }

	            @Override
	            public boolean onReSuccess(String result) {

	                ResBody body = resovleJson(result,ResBody.class);
	                boolean reBoolean = true;

	                if(body == null){

	                    reBoolean = onListener.error(
	                        -1,
	                        SERVICE_ERROR_DATA,
	                        IOnProxyDoneListener.ERR_TYPE_SERVICE);
	                }else if(checkServiceState(body.State)){

	                    onListener.success(result);
	                }else{

	                    reBoolean = onListener.error(
	                        body.State,
	                        body.Message,
	                        IOnProxyDoneListener.ERR_TYPE_SERVICE);
	                }

	                onListener.done();
	                return reBoolean;

	            }
	        
	            @Override
	            public boolean onReError(int stateCode, String message) {
	                boolean reBoolean = onListener.error(stateCode, message, IOnProxyDoneListener.ERR_TYPE_NET);
	                onListener.done();
	                return reBoolean;
	            }
	            
	            @Override
	            public void onError(int stateCode, String message) {
	                onReError(stateCode, message);
	            }
         };
	        
         Promiser $q;
         if(method.toUpperCase().equals("GET")){
             $q = provider.get(pContext,
             null,
             pInput,
             l);
         }else{
             $q = provider.post(pContext,
             null,
             pInput,
             l);
         }

         onListener.setPromiser($q);
         return $q;
     }
	    
	public Promiser $doRequest(
	    IProxyDataProvider provider,
	    GetCircleRangePInput pInput,
	    final IOnProxyDoneListener_v3<GetCircleRangeOutput> onListener
	){

	    String url = getApiURL();
	    ProviderContext pContext = getPContext();
	    pContext.setHost(url);
	    
	    IHttpHelperListener_v4 l = new IHttpHelperListener_v4() {
	            @Override
	            public void onSuccess(String result) {

	                onReSuccess(result);

	            }

	            @Override
	            public boolean onReSuccess(String result) {

	                ResBody body = resovleJson(result,ResBody.class);

	                boolean reBoolean = true;
	                if(body == null){

	                    reBoolean = onListener.error(
	                        -1,
	                        SERVICE_ERROR_DATA,
	                        IOnProxyDoneListener.ERR_TYPE_SERVICE);
	                }else if(checkServiceState(body.State)){

	                    GetCircleRangeOutput output = resovleJson(result,GetCircleRangeOutput.class);
	                    onListener.success(output);
	                }else{

	                    reBoolean = onListener.error(
	                        body.State,
	                        body.Message,
	                        IOnProxyDoneListener.ERR_TYPE_SERVICE);
	                }

	                onListener.done();
	                return reBoolean;

	            }
	        
	            @Override
	            public boolean onReError(int stateCode, String message) {
	                boolean reBoolean = onListener.error(stateCode, message, IOnProxyDoneListener.ERR_TYPE_NET);
	                onListener.done();
	                return reBoolean;
	            }
	            
	            @Override
	            public void onError(int stateCode, String message) {
	                onReError(stateCode, message);
	            }
         };
	        
         Promiser $q;
         if(method.toUpperCase().equals("GET")){
             $q = provider.get(pContext,
             null,
             pInput,
             l);
         }else{
             $q = provider.post(pContext,
             null,
             pInput,
             l);
         }

         onListener.setPromiser($q);
         return $q;
     }
	    
     public String getApiURL(){
         return getHost() + apiUrl;
     }
	    
     public String getAction(){
         return null;
     }

}
